<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="园区" prop="parkId">
        <el-select clearable v-model="queryParams.parkId" @change="getBuildingList(form.parkId)" placeholder="请选择园区">
          <el-option
            v-for="item in buiParkList"
            :key="item.id"
            :label="item.parkName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="楼栋" prop="buildingId">
        <el-select
          clearable
          v-model="queryParams.buildingId"
          placeholder="请选择楼栋"
        >
          <el-option
            v-for="item in buildingList"
            :key="item.id"
            :label="item.buildingName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable size="small">
          <el-option label="请选择字典生成" value=""/>
        </el-select>
      </el-form-item>
      <el-form-item label="生产厂家" prop="factory">
        <el-input
          v-model="queryParams.factory"
          placeholder="请输入生产厂家"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出厂日期">
        <el-date-picker
          v-model="daterangeFactoryDate"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['security:afExtinguisher:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['security:afExtinguisher:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['security:afExtinguisher:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['security:afExtinguisher:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="afExtinguisherList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编码" align="center" prop="devCode"/>
      <el-table-column label="名称" align="center" prop="devName"/>
      <el-table-column label="规格" align="center" prop="specification"/>
      <el-table-column label="类型" align="center" prop="type"/>
      <el-table-column label="数量" align="center" prop="number"/>
      <el-table-column label="生产厂家" align="center" prop="factory"/>
      <el-table-column label="出厂日期" align="center" prop="factoryDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.factoryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="园区" align="center" prop="parkName"/>
      <el-table-column label="楼栋" align="center" prop="buildingName"/>
      <el-table-column label="位置" align="center" prop="location"/>
      <el-table-column label="使用年限" align="center" prop="useYear"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['security:afExtinguisher:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['security:afExtinguisher:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改灭火器台账对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="资产类型" v-if="!form.id" prop="sysBmId">
          <!--          <BmGenSelect @select="genCode"></BmGenSelect>-->
          <treeselect v-model="form.sysBmId" :options="sysBmTypeOptions" :normalizer="bmNormalizer"
                      @select="genCode" placeholder="选择资产类型"/>
        </el-form-item>
        <el-form-item label="名称" prop="devName">
          <el-input v-model="form.devName" placeholder="请输入名称"/>
        </el-form-item>
        <el-form-item label="规格" prop="specification">
          <el-input v-model="form.specification" placeholder="请输入规格"/>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">

            <el-option v-for="item in extinguisherOptions" :key="item.dictValue" :label="item.dictValue"
                       :value="item.dictValue">
            </el-option>

          </el-select>
        </el-form-item>
        <el-form-item label="数量" prop="number">
          <el-input v-model="form.number" placeholder="请输入数量"/>
        </el-form-item>
        <el-form-item label="生产厂家" prop="factory">
          <el-input v-model="form.factory" placeholder="请输入生产厂家"/>
        </el-form-item>
        <el-form-item label="出厂日期" prop="factoryDate">
          <el-date-picker clearable size="small"
                          v-model="form.factoryDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择出厂日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="园区" prop="parkId">
          <el-select clearable v-model="form.parkId" @change="getBuildingList(form.parkId)" placeholder="请选择园区">
            <el-option
              v-for="item in buiParkList"
              :key="item.id"
              :label="item.parkName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼栋" prop="buildingId">
          <el-select
            clearable
            v-model="form.buildingId"
            placeholder="请选择楼栋"
          >
            <el-option
              v-for="item in buildingList"
              :key="item.id"
              :label="item.buildingName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入位置"/>
        </el-form-item>
        <el-form-item label="使用年限" prop="useYear">
          <el-input v-model="form.useYear" placeholder="请输入使用年限"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAfExtinguisher,
  getAfExtinguisher,
  delAfExtinguisher,
  addAfExtinguisher,
  updateAfExtinguisher
} from '@/api/smartParks/security/securitySafetyExtinguisher'
import { listBuiPark } from '@/api/parkAssets/enterCommunity'
import { listBuiBuilding } from '@/api/parkAssets/building'

export default {
  name: 'AfExtinguisher',
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 灭火器台账表格数据
      afExtinguisherList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      sysBmTypeOptions: this.$store.state.permission.sysBmTree,
      // 出厂日期时间范围
      daterangeFactoryDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        factory: null,
        factoryDate: null,
        parkId: null,
        buildingId: null,
        location: null
      },
      buiParkList: [],
      buildingList: [],
      extinguisherOptions: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
    listBuiPark().then((response) => {
      this.buiParkList = response.rows
    })
    this.getDicts('extinguisher_type').then(response => {
      this.extinguisherOptions = response.data
    })

  },
  methods: {

    genCode(item) {
      this.form.sysBmId = item.bmId
    },

    //得到楼栋
    getBuildingList(value) {
      this.form.buildingId = ''
      this.buildingList = []
      listBuiBuilding({ parkId: value }).then((response) => {
        this.buildingList = response.rows
      })
    },

    /** 查询灭火器台账列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangeFactoryDate && '' != this.daterangeFactoryDate) {
        this.queryParams.params['beginFactoryDate'] = this.daterangeFactoryDate[0]
        this.queryParams.params['endFactoryDate'] = this.daterangeFactoryDate[1]
      }
      listAfExtinguisher(this.queryParams).then(response => {
        this.afExtinguisherList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        delFlag: null,
        devCode: null,
        devName: null,
        specification: null,
        type: null,
        factory: null,
        factoryDate: null,
        parkId: null,
        buildingId: null,
        location: null,
        useYear: null,
        devStatus: '0',
        remark: null,
        number: null,
        sysBmId: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeFactoryDate = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加灭火器台账'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getAfExtinguisher(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改灭火器台账'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAfExtinguisher(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addAfExtinguisher(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除灭火器台账编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delAfExtinguisher(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('security/afExtinguisher/export', {
        ...this.queryParams
      }, `security_afExtinguisher.xlsx`)
    }
  }
}
</script>
